package io.renren.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.SqlHelper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.toolkit.StringUtils;
import io.renren.constant.AppConstant;
import io.renren.dao.PaomadengDao;
import io.renren.entity.Paomadeng;
import io.renren.vo.RespModel;
import io.renren.service.PaomadengService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PaomadengServiceImpl implements PaomadengService {

    @Autowired
    private PaomadengDao paomadengMapper;

    public RespModel<List<Paomadeng>> selectAll(Paomadeng paomadengPage) {
        EntityWrapper<Paomadeng> wrapper = new EntityWrapper<>();
        String type = paomadengPage.getType();
        String name = paomadengPage.getName();
        wrapper.eq(StringUtils.checkValNotNull(type), "type", paomadengPage.getType());
        wrapper.like(StringUtils.checkValNotNull(name), "name", paomadengPage.getName());
        int count ;
        List<Paomadeng> paomadengs;
        if(StringUtils.checkValNotNull(paomadengPage.getStart()) && StringUtils.checkValNotNull(paomadengPage.getLimit())) {
            Page page = new Page<Paomadeng>(paomadengPage.getStart(), paomadengPage.getLimit());
            Wrapper wrapper1 = SqlHelper.fillWrapper(page, wrapper);
            paomadengs = paomadengMapper.selectPage(page, wrapper1);
            count = page.getTotal();
        }else {
            paomadengs = paomadengMapper.selectList(wrapper);
            count = paomadengs.size();
        }
        return new RespModel<>(AppConstant.STATU_SUCCESS.getName(), AppConstant.STATU_SUCCESS.getValue(), String.valueOf(count), paomadengs);
    }

}
